//
//  RecommendContentViewMaker.m
//  nonghubao
//
//  Created by 王 文彬 on 13-3-10.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "RecommendContentViewMaker.h"
#import "DynamicPicturesView.h"
#import "DynamicFooterView.h"
#import "Config.h"
#import "ProductContentViewMaker.h"
#import <QuartzCore/QuartzCore.h>
#import "RTLabel.h"

@implementation RecommendContentViewMaker

+ (void)makePrototype:(UIView *)view
{
    //
    if (view) {
        //推荐内容
        UITextView* contentView = [[UITextView alloc] initWithFrame:CGRectMake(10, 5, 280, 20)];
        contentView.backgroundColor = [UIColor clearColor];
        contentView.font = _SYS_FONT_002;
        contentView.editable = false;
        contentView.scrollEnabled = false;
        [view addSubview:contentView];
        
        contentView.contentInset = UIEdgeInsetsMake(-8, -8, 0, 0);
        if (iOS7) {
            contentView.contentInset = UIEdgeInsetsMake(-8, 0, 0, 0);
        }
    
        //商品信息的背景图片
        UIView* outlineView = [[UIView alloc] initWithFrame:CGRectMake(5, 27, 290, 40)];
        outlineView.layer.cornerRadius = 5;
        outlineView.layer.borderWidth = 1;
        outlineView.layer.borderColor = [_SYS_COLOR_005 CGColor];
        [view addSubview:outlineView];
        
        
        //商品信息view
        UIView* productView = [[UIView alloc] initWithFrame:CGRectMake(5, outlineView.frame.origin.x + 5, 280, 0)];
        productView.backgroundColor = [UIColor clearColor];
        [ProductContentViewMaker makePrototype:productView];
        [view addSubview:productView];
        
        //商品信息footerview
        UIView* smallFooterLabel = [productView.subviews objectAtIndex:7];
        smallFooterLabel.frame = CGRectMake(smallFooterLabel.frame.origin.x + 70, smallFooterLabel.frame.origin.y, smallFooterLabel.frame.size.width, smallFooterLabel.frame.size.height);
        
        //footer
        RTLabel* footerLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, outlineView.frame.origin.y + outlineView.frame.size.height + 20, 280, 18)];
        footerLabel.font = _SYS_FONT_002;
        [view addSubview:footerLabel];
//        UIView* footerView = [[DynamicFooterView alloc] initWithFrame:CGRectMake(5, outlineView.frame.origin.y + outlineView.frame.size.height + 10, 280, 18) forStyle:FooterViewStyleMultiBig];
//        footerView.backgroundColor = [UIColor clearColor];
//        [view addSubview:footerView];
        
        //箭头
        UIImageView* arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 25, 13, 9)];
        arrowImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_icon_012@2x" ofType:@"png"]];
//        arrowImageView.backgroundColor = [UIColor redColor];
        [view addSubview:arrowImageView];
    }
}


// 计算推荐动态的高度
// 高度为：推荐内容 + 产品视图 + 背景视图边框 * 2 + footer
+ (float)heightWithDictionary:(NSDictionary *)data
{
    float height = 10.0f;
    
    //推荐
    NSString* recommendContent = data[@"content"];
    if (recommendContent.length > 0) {
        
        CGSize size = [recommendContent sizeWithFont:_SYS_FONT_002 constrainedToSize: CGSizeMake(260.0f, 100.0f) lineBreakMode:UILineBreakModeWordWrap];
        height += size.height > 20 ? size.height : 20;
    }
    
    height += [ProductContentViewMaker heightWithDictionary:data];
    
    //背景视图边框高度*2
    height += 10 * 2;
    
    //footer
    height += 18;
    
    //间隙
    height += 5 + 5;

    return height;
}


// 根据数据重新绘制动态内容
+ (void)drawContentView:(UIView *)view withDictionary:(NSDictionary *)data
{
    //获取文字内容
    NSString* content = data[@"content"];
    //获取文字label
    UITextView* contentView = view.subviews[0];
    contentView.text = content ? content : @"";
    
    //由于文字部分高度不定，所以需要在cell重用后向cell中加入文字部分
    //先设置动态内容部分view的高度，再设置其子view的高度，即文字说明的高度
    CGSize size = [contentView.text sizeWithFont:_SYS_FONT_002 constrainedToSize: CGSizeMake(260.0f, 100.0f) lineBreakMode:UILineBreakModeWordWrap];
    contentView.frame = CGRectMake(contentView.frame.origin.x, contentView.frame.origin.y, 280, size.height + 10);

    
    //取出商品对象
    NSDictionary* product = data[@"product"];

    //商品信息视图
    UIView* productView = view.subviews[2];
    [ProductContentViewMaker drawContentView:productView withDictionary:data];
    productView.frame = CGRectMake(productView.frame.origin.x, contentView.frame.origin.x + contentView.frame.size.height + 10, productView.frame.size.width, productView.frame.size.height);
    
    //先重设背景图的frame
    UIView* outlineView = view.subviews[1];
    outlineView.frame = CGRectMake(outlineView.frame.origin.x, contentView.frame.origin.y + contentView.frame.size.height + 7, outlineView.frame.size.width, productView.frame.size.height + 10);
    
    //重画箭头的位置，根据outlineView来确定位置坐标
    UIImageView* arrowImageView = view.subviews[4];
    arrowImageView.frame = CGRectMake(20, outlineView.frame.origin.y - 7, arrowImageView.frame.size.width, arrowImageView.frame.size.height);
    

    //footer
    //设定footer的计数值。要注意，如果product是NSNull，那么trasactionCount和recommendcount需要判断，并处理为0
    int commented = [data[@"commentCount"] integerValue];
    int sold = [product isKindOfClass:[NSNull class]] ? 0 : [product[@"transactionCount"] integerValue];
    int recommended = [product isKindOfClass:[NSNull class]] ? 0 : [product[@"recommendCount"] integerValue];
    
    RTLabel* footerLabel = view.subviews[3];
    footerLabel.frame = CGRectMake(footerLabel.frame.origin.x, outlineView.frame.origin.y + outlineView.frame.size.height + 15, footerLabel.frame.size.width, footerLabel.frame.size.height);
    footerLabel.text = [NSString stringWithFormat:_L(@"tpl_33"), commented, recommended, sold];
    
//    DynamicFooterView* footerView = view.subviews[3];
//    [footerView setNumbersWithCommented:commented andSold:sold andRecommended:recommended];
    
    //重绘自己视图的|高度|
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, footerLabel.frame.origin.y + footerLabel.frame.size.height);
}


@end
